package cn.mcch.investigator.presenter.impl;

import android.text.TextUtils;

import java.util.LinkedHashMap;
import java.util.Map;
import java.util.regex.Pattern;

import cn.mcch.investigator.R;
import cn.mcch.investigator.listener.OnResponseListener;
import cn.mcch.investigator.model.impl.LoginModelImpl;
import cn.mcch.investigator.presenter.BasePresenter;
import cn.mcch.investigator.presenter.ILoginPresenter;
import cn.mcch.investigator.utils.ConstantUtil;
import cn.mcch.investigator.utils.SPUtils;
import cn.mcch.investigator.utils.ToastUtils;
import cn.mcch.investigator.viewdelegate.impl.LoginViewDelegateImpl;

/**
 * Created by Administrator on 2016/12/13.
 */
public class LoginPresenterImpl extends BasePresenter<LoginViewDelegateImpl, LoginModelImpl> implements ILoginPresenter {
    @Override
    public Class<LoginViewDelegateImpl> getViewDelegateClass() {
        return LoginViewDelegateImpl.class;
    }

    @Override
    public Class<LoginModelImpl> getModelClass() {
        return LoginModelImpl.class;
    }

    @Override
    public boolean verify() {
        String username = mViewDelegate.getText(R.id.se_username);
        String password = mViewDelegate.getText(R.id.se_password);
        if (TextUtils.isEmpty(username)) {
            ToastUtils.showToast("手机号码不能为空");
            return false;
        }
        if (TextUtils.isEmpty(password)) {
            ToastUtils.showToast("密码不能为空");
            return false;
        }

        if (!Pattern.matches("^1\\d{10}$", username)) {
            ToastUtils.showToast("手机号码格式不准确，请检查");
            return false;
        }
        return true;
    }

    @Override
    public void login(OnResponseListener l) {
        String username = mViewDelegate.getText(R.id.se_username);
        String password = mViewDelegate.getText(R.id.se_password);
        Map<String, String> prarms = new LinkedHashMap<String, String>();
        prarms.put("account", username);
        prarms.put("pwd", password);
        mModel.login(1,ConstantUtil.ACT_LOGIN, prarms, l);
    }

    @Override
    public void saveUsername() {
        SPUtils.putString("username", mViewDelegate.getText(R.id.se_username));
    }

    @Override
    public void setUsername() {
        mViewDelegate.setText(R.id.se_username, SPUtils.getString("username", ""));
        mViewDelegate.setText(R.id.se_password, "qwe123");
    }

}
